package tommysmapeditor.views;

import java.io.File;
import java.io.IOException;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import tommysmapeditor.editors.AnimationAndHitboxEditor;
import tommysmapeditor.editors.MapEditor;
import tommysmapeditor.editors.bo.TileMapFormat;
import tommysmapeditor.helper.importer.ImporterFactory;
import tommysmapeditor.helper.importer.JakoFormatImporter;
import tommysmapeditor.helper.importer.TommyFormatImporter;
import tommysmapeditor.input.MapEditorInput;

public class Summary extends ViewPart {

//	int DEFAULT_TILE_WIDTH = 256;
//	int DEFAULT_TILE_HEIGTH = 64;
	private Button mapEditor;
	private Button animHBEd;
//	private Text tileWidth;
//	private Text tileHeight;
	private Combo tileMapFormat;
	
	
	public Summary() {
		
		
		
	}

	@Override
	public void createPartControl(Composite parent) {

		parent.setLayout(new FormLayout());
		
//		tileWidth = new Text(parent, SWT.BORDER);
//		tileWidth.setText(String.valueOf(DEFAULT_TILE_WIDTH));
		
//		FormData fd_width=new FormData();
//		fd_width.left = new FormAttachment(0,10);
//		tileWidth.setLayoutData(fd_width);
//		
//		tileHeight = new Text(parent, SWT.BORDER);
//		tileHeight.setText(String.valueOf(DEFAULT_TILE_HEIGTH));
//
//		FormData fd_height=new FormData();
//		fd_height.left = new FormAttachment(tileWidth,10);
//		tileHeight.setLayoutData(fd_height);
		
		
		mapEditor = new Button(parent,SWT.NONE);
		mapEditor.setText("map editor load");
		mapEditor.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void widgetSelected(SelectionEvent e) {
				try {
					openMapEditor();
				} catch (PartInitException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			
		});

		FormData fd_me=new FormData();
//		fd_me.left = new FormAttachment(tileHeight,10);
		fd_me.left = new FormAttachment(0,10);
		mapEditor.setLayoutData(fd_me);

		
		

		
		animHBEd = new Button(parent,SWT.NONE);
		animHBEd.setText("anim hitbox editor");
//		animHBEd.setBounds(800, 10, 150, 200);
		animHBEd.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent e) {
				try {
					getSite().getPage().openEditor(
							new IEditorInput(){

								public boolean exists() {
									// TODO Auto-generated method stub
									return false;
								}

								public ImageDescriptor getImageDescriptor() {
									// TODO Auto-generated method stub
									return null;
								}

								public String getName() {
									return "";
								}

								public IPersistableElement getPersistable() {
									// TODO Auto-generated method stub
									return null;
								}

								public String getToolTipText() {
									return "";
								}

								public Object getAdapter(Class adapter) {
									// TODO Auto-generated method stub
									return null;
								}
								
							}
					,AnimationAndHitboxEditor.ID);
				} catch (PartInitException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

		FormData fd_ahe=new FormData();
		fd_ahe.left = new FormAttachment(0,10);
//		fd_ahe.left = new FormAttachment(tileHeight,10);
		fd_ahe.top = new FormAttachment(mapEditor,10);
		animHBEd.setLayoutData(fd_ahe);

		
		
		tileMapFormat = new Combo(parent,SWT.READ_ONLY);
		tileMapFormat.setItems(TileMapFormat.getComboItems());
		
		FormData fd_tmf = new FormData();
		fd_tmf.left = new FormAttachment(mapEditor,10);
		fd_tmf.top=new FormAttachment(0,0);
		
		tileMapFormat.setLayoutData(fd_tmf);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	private void openMapEditor() throws PartInitException {
		
		TileMapFormat format = TileMapFormat.valueOf(tileMapFormat.getText());
		
		
		FileDialog fd = new FileDialog(Display.getCurrent().getActiveShell());
		String tileMapName = fd.open();
		
		System.out.println("map file selected:"+tileMapName);

		
		
		ImporterFactory factory =null;
		
		if(format == TileMapFormat.JAKO){
			factory = new JakoFormatImporter();
		}else if(format == TileMapFormat.THOMAS){
			factory = new TommyFormatImporter();
		}
		
		try {
			getSite().getPage().openEditor(
					factory.translate(tileMapName)
			, MapEditor.ID);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
